<template>
	<view class="content">
		<view class="loginBg">
			<view class="contentBox">
				<image :src="baseUrl+data.mlogo" mode=""></image>
				<view class="" style="font-size: 20px;">
					{{data.title ? data.title : '沥青管理系统'}}
				</view>
			</view>
		</view>
		<view class="loginBox">
			<view class="login">
				<view v-if="!isPhone">
					<form class="cl">
						<view class="t-a">
							<image src="../../static/img/wodeW.png"></image>
							<view class="line"></view>
							<input placeholder="请输入账号" v-model="formData.account" />
						</view>
						<view class="t-a">
							<image src="https://zhoukaiwen.com/img/loginImg/pwd.png"></image>
							<view class="line"></view>
							<input type="password" placeholder="请输入登录密码" v-model="formData.passwd" />
						</view>
					</form>
					<view style="width:90%;margin: 15px auto;text-align: left;">
						<u-checkbox-group size="30" iconSize="30">
							<u-checkbox v-model="rememberPswd" label="记住账户密码" :checked="true" labelSize="30">
							</u-checkbox>
						</u-checkbox-group>
					</view>
				</view>
				<view v-else>
					<form class="cl">
						<view class="t-a">
							<image src="../../static/img/sj.png"></image>
							<view class="line"></view>
							<input type="number" name="phone" placeholder="请输入手机号" maxlength="11"
								v-model="form.phone" />
						</view>
						<view class="t-a">
							<image src="../../static/img/yz.png"></image>
							<view class="line"></view>
							<input type="number" name="code" maxlength="6" placeholder="请输入验证码" v-model="form.code" />
							<view v-if="showText" class="t-c" @click="getCode()">发送验证码</view>
							<view v-else class="t-c" style="background-color: #A7A7A7;">重新发送({{ second }})</view>
						</view>
					</form>
				</view>
				<view class="t-b2" @click="isPhone = !isPhone">切换登录</view>
				<view class="loginBnt" @click="login()">
					登录
				</view>
			</view>
		</view>
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>

<script>
	import {
		login,
		loginPhone,
		getSms,
		getLogo
	} from "@/api/login.js"
	import {
		baseUrl
	} from '@/api/config.js'
	export default {
		data() {
			return {
				formData: {
					account: '',
					passwd: ''
				},
				form: {
					phone: '',
					code: '',
				},
				rememberPswd: true,
				isPhone: false,
				second: 60, //默认60秒
				showText: true, //判断短信是否发送
				data:{},
				baseUrl:''
			}
		},
		onLoad() {
			if(uni.getStorageSync("guid")){
				uni.switchTab({
					url: '/pages/home/index',
				});
			}
		},
		created() {
			this.baseUrl = baseUrl
			this.getLogo()
			this.formData.account = uni.getStorageSync("account");
			this.formData.passwd = uni.getStorageSync("passwd");
		},
		methods: {
			async getLogo() {
				const {
					data
				} = await getLogo()
				this.data = data.data
			},
			//获取短信验证码
			getCode() {
				if (!this.form.phone) {
					console.log("11111111111")
					this.$refs.uToast.show({
						type: 'warning',
						icon: false,
						title: '提示',
						message: "请输入手机号",
					});
				} else if (!/^[1][3,4,5,7,8,9][0-9]{9}$/.test(this.form.phone)) {
					console.log("22222222222")
					this.$refs.uToast.show({
						type: 'warning',
						icon: false,
						title: '提示',
						message: "请输入正确手机号",
					});
				} else {
					console.log("3333333333")
					var that = this;
					var interval = setInterval(() => {
						that.showText = false;
						var times = that.second - 1;
						//that.second = times<10?'0'+times:times ;//小于10秒补 0
						that.second = times;
					}, 1000);
					setTimeout(() => {
						clearInterval(interval);
						that.second = 60;
						that.showText = true;
					}, 60000);
					//获取短信验证码
					getSms({
						phone: this.form.phone
					}).then(res => {
						console.log("获取验证码",res)
					})
				}

			},
			login() {
				if (this.isPhone) {
					if (!this.form.phone) {
						this.$refs.uToast.show({
							type: 'warning',
							icon: false,
							title: '提示',
							message: "请输入手机号",
						});
					} else if (!/^[1][3,4,5,7,8,9][0-9]{9}$/.test(this.form.phone)) {
						this.$refs.uToast.show({
							type: 'warning',
							icon: false,
							title: '提示',
							message: "请输入正确手机号",
						});
					} else if (!this.form.code) {
						this.$refs.uToast.show({
							type: 'warning',
							icon: false,
							title: '提示',
							message: "请输入验证码",
						});
					} else {
						loginPhone(this.form).then((res) => {
							if (res.data.code === 1) {
								uni.setStorageSync("guid", res.data.data._login.guid);
								uni.switchTab({
									url: '/pages/home/index',
								});
							} else {
								this.$refs.uToast.show({
									title: res.data.error,
									type: 'error ',
								})
							}
						})
					}

				} else {
					if (!this.formData.account) {
						this.$refs.uToast.show({
							type: 'warning',
							icon: false,
							title: '提示',
							message: "请输入账号",
						});
					} else if (!this.formData.passwd) {
						this.$refs.uToast.show({
							type: 'warning',
							icon: false,
							title: '提示',
							message: "请输入密码",
						});
					} else {
						login(this.formData).then((res) => {
							if (res.data.code === 1) {
								if (this.rememberPswd) {
									uni.setStorageSync("account", this.formData.account);
									uni.setStorageSync("passwd", this.formData.passwd);
								} else {
									uni.setStorageSync("account", '');
									uni.setStorageSync("passwd", '');
								}
								console.log(res.data.data.id)
								uni.setStorageSync("guid", res.data.data._login.guid);
								uni.setStorageSync("userId", res.data.data.id);
								uni.switchTab({
									url: '/pages/home/index',
								});
							} else {
								this.$refs.uToast.show({
									title: res.data.error,
									type: 'error ',
								})
							}
						})
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		position: relative;
	}

	.loginBg {
		width: 100%;
		height: 300px;
		background: url(../../static/img/bg.png)no-repeat;
		background-size: 100% 100%;
		text-align: center;
		color: #fff;
		font-size: 25px;

		.contentBox {
			padding-top: 16%;
		}

		image {
			width: 100px;
			height: 55px;
		}
	}

	.loginBox {
		width: 100%;
		position: absolute;
		top: 60%;

		.login {
			width: 85%;
			margin: 0 auto;
			padding: 20% 0px;
			text-align: center;
			background: #FFFFFF;
			box-shadow: 0px 10px 40px 0px rgba(223, 223, 223, 0.91);
			border-radius: 20px;
			z-index: 9;
			padding-bottom: 30%;

			.loginBnt {
				width: 85%;
				height: 42.5px;
				line-height: 42.5px;
				margin: 0 auto;
				text-align: center;
				background: url('../../static/img/btn.png')no-repeat;
				background-size: 100% 100%;
				color: #fff;
				margin: 10% auto;

			}

			.u-field {
				width: 90%;
				margin: 0 auto;
				color: #ACACAC;
				border: none;
				background: #F3F4F5;
				margin-bottom: 10px;
				border-radius: 5px;

				.u-icon-wrap .u-iconfont {
					color: #005AAD !important;
				}

			}

			.t-b2 {
				text-align: right;
				width: 90%;
				color: #0062ad;
				font-size: 17px;
			}

			.t-a {
				position: relative;
				width: 90%;
				margin: 0 auto;
			}

			.t-a image {
				width: 40rpx;
				height: 40rpx;
				position: absolute;
				left: 40rpx;
				top: 28rpx;
				/* border-right: 2rpx solid #dedede; */
				margin-right: 20rpx;
			}

			.t-a input {
				padding: 0 20rpx 0 120rpx;
				height: 80rpx;
				line-height: 90rpx;
				margin-bottom: 50rpx;
				background: #f8f7fc;
				border: 1px solid #e9e9e9;
				font-size: 28rpx;
				border-radius: 50rpx;
				text-align: left;
			}

			.t-a .line {
				width: 2rpx;
				height: 40rpx;
				background-color: #dedede;
				position: absolute;
				top: 28rpx;
				left: 98rpx;
			}

			.t-c {
				position: absolute;
				right: 22rpx;
				top: 22rpx;
				background: #5677fc;
				color: #fff;
				font-size: 24rpx;
				border-radius: 50rpx;
				height: 50rpx;
				line-height: 50rpx;
				padding: 0 25rpx;
			}
		}
	}
</style>
<style>
	.u-field .u-iconfont {
		color: #0062AD !important;
		font-size: 25px;
	}
</style>
